<template>
  <div class="app-container">
    <el-tabs type="border-card" @tab-click="tabClick">
      <el-tab-pane label="资料小类">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['system:post:create']"
            style="margin-right: 10px;">新增</el-button>
          <el-button type="primary" size="mini" @click="handleQuery" v-hasPermi="['system:post:create']"
            style="margin-right: 20px;">查询</el-button>
          <el-form-item label="资料大类" prop="dataCategory">
            <el-select v-model="queryParams.dataCategory" filterable allow-create default-first-option
              placeholder="请选择资料大类名称" style="width:100%" @change="bigTypechange">
              <el-option v-for="item in bigOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="资料小类" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入资料小类名称" clearable />
          </el-form-item>

        </el-form>
        <el-table v-loading="loading" :data="postList">
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="资料大类" align="center" prop="dataCategory">
            <template v-slot="scope">
              <span v-if="scope.row.dataCategory == 1">CT扫描</span>
              <span v-if="scope.row.dataCategory == 2">电镜扫描</span>
              <span v-if="scope.row.dataCategory == 3">矿物定量扫描</span>
              <span v-if="scope.row.dataCategory == 4">冷冻电镜</span>
            </template>
          </el-table-column>
          <el-table-column label="资料小类" align="center" prop="name" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:post:update']">编辑</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:post:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="解析参数">
        <el-form :model="queryParams1" ref="queryForm1" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-button type="primary" size="mini" @click="handleAdd1" v-hasPermi="['system:post:create']"
            style="margin-right: 10px;">新增</el-button>
          <el-button type="primary" size="mini" @click="handleQuery1" v-hasPermi="['system:post:create']"
            style="margin-right: 20px;">查询</el-button>
          <el-form-item label="资料大类" prop="dataCategory">
            <el-select v-model="queryParams1.dataCategory" filterable allow-create default-first-option
              placeholder="请选择资料大类名称" style="width:100%" @change="bigTypechange">
              <el-option v-for="item in bigOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="解析参数" prop="name">
            <el-input v-model="queryParams1.name" placeholder="请输入解析参数名称" clearable />
          </el-form-item>

        </el-form>
        <el-table v-loading="loading" :data="postList1">
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="资料大类" align="center" prop="dataCategory">
            <template v-slot="scope">
              <span v-if="scope.row.dataCategory == 1">CT扫描</span>
              <span v-if="scope.row.dataCategory == 2">电镜扫描</span>
              <span v-if="scope.row.dataCategory == 3">矿物定量扫描</span>
              <span v-if="scope.row.dataCategory == 4">冷冻电镜</span>
            </template>
          </el-table-column>
          <el-table-column label="解析参数" align="center" prop="name" />
          <el-table-column label="参数编码" align="center" prop="code" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate1(scope.row)"
                v-hasPermi="['system:post:update']">编辑</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete1(scope.row)"
                v-hasPermi="['system:post:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>


    <!-- 添加或修改资料小类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资料大类" prop="dataCategory">
          <el-select v-model="form.dataCategory" filterable allow-create default-first-option placeholder="请选择"
            style="width:100%" @change="bigTypechange" :disabled="infoStatue">
            <el-option v-for="item in bigOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资料小类" prop="name">
          <el-input v-model="form.name" placeholder="请输入资料小类名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改解析参数对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="80px">
        <el-form-item label="资料大类" prop="dataCategory">
          <el-select v-model="form1.dataCategory" filterable allow-create default-first-option placeholder="请选择"
            style="width:100%" @change="bigTypechange1" :disabled="parameterStatue">
            <el-option v-for="item in bigOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数编码" prop="code">
          <el-input v-model="form1.code" placeholder="请输入解析参数编码" />
        </el-form-item>
        <el-form-item label="解析参数" prop="name">
          <el-input v-model="form1.name" placeholder="请输入解析参数名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, delPost, addPost, updatePost, listPost1, delPost1, addPost1, updatePost1, } from "@/api/system/post";

export default {
  name: "Post",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 岗位表格数据
      postList: [],
      postList1: [],
      // 弹出层标题
      title: "",
      title1: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        dataCategory: undefined,
        name: '',
      },
      queryParams1: {
        dataCategory: undefined,
        name: '',
      },
      // 表单参数
      form: { name: '', dataCategory: null },
      form1: { name: '', dataCategory: null, code: null },
      // 表单校验
      rules: {
        dataCategory: [
          { required: true, message: "资料大类名称不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "资料小类不能为空", trigger: "blur" }
        ],
      },
      rules1: {
        dataCategory: [
          { required: true, message: "资料大类名称不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "解析参数不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "参数编码不能为空", trigger: "blur" }
        ],
      },
      bigOptions: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: 'CT扫描'
        },
        {
          value: 2,
          label: '电镜扫描'
        },
        {
          value: 3,
          label: '矿物定量扫描'
        },
        {
          value: 4,
          label: '冷冻电镜'
        },
      ],
      infoStatue: false,
      parameterStatue: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tabClick(info) {
      console.log(info.label)
      if (info.label == '资料小类') {
        this.getList();
      } else if (info.label == '解析参数') {
        this.getList1();
      }
    },
    bigTypechange(e) {
      console.log(e, 111111111)



    },
    bigTypechange1() {

    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.data;
        this.loading = false;
      });
    },
    getList1() {
      this.loading = true;
      listPost1(this.queryParams1).then(response => {
        this.postList1 = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        dataCategory: undefined,

      };
      this.resetForm("form");
    },
    reset1() {
      this.form1 = {
        id: undefined,
        name: undefined,
        dataCategory: undefined,

      };
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    handleQuery1() {
      this.getList1();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.infoStatue = false
      this.title = "添加资料小类";
    },
    handleAdd1() {
      this.reset();
      this.open1 = true;
      this.parameterStatue = false
      this.title = "添加解析参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form.dataCategory = row.dataCategory
      this.form.name = row.name
      this.form.id = row.id
      this.open = true;
      this.infoStatue = true
      this.title = "修改资料小类";
    },
    handleUpdate1(row) {
      this.reset();
      const id = row.id
      this.open1 = true;
      this.parameterStatue = true
      this.form1.dataCategory = row.dataCategory
      this.form1.name = row.name
      this.form1.id = row.id
      this.form1.code = row.code
      this.title = "修改解析参数";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            let info = {
              dataCategory: this.form.dataCategory,
              name: this.form.name,
            }
            updatePost(info, this.form.id).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm1: function () {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form1.id !== undefined) {
            updatePost1(this.form1, this.form1.id).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.getList1();
            });
          } else {
            addPost1(this.form1).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList1();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除该资料小类?').then(function () {
        return delPost(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleDelete1(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除该解析参数?').then(function () {
        return delPost1(ids);
      }).then(() => {
        this.getList1();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
